DsEnumerateDomainTrusts (netapi32)
Last changed: Chorlton The Dragon-217.40.165.217

.
Summary
The DsEnumerateDomainTrusts function obtains domain trust data for a specified domain.

C# Signature:

[DllImport("Netapi32.dll", CallingConvention=CallingConvention.Winapi, SetLastError=true, CharSet=CharSet.Auto)]
private static extern uint DsEnumerateDomainTrusts(string ServerName,
                            uint Flags,
                            out IntPtr Domains,
                            out uint DomainCount);

VB Signature:

Declare Function DsEnumerateDomainTrusts Lib "netapi32.dll" (TODO) As TODO

User-Defined Types:

Flags

private enum DS_DOMAIN_TRUST_TYPE : uint
{
     DS_DOMAIN_IN_FOREST       = 0x0001,  // Domain is a member of the forest
     DS_DOMAIN_DIRECT_OUTBOUND     = 0x0002,  // Domain is directly trusted
     DS_DOMAIN_TREE_ROOT       = 0x0004,  // Domain is root of a tree in the forest
     DS_DOMAIN_PRIMARY         = 0x0008,  // Domain is the primary domain of queried server
     DS_DOMAIN_NATIVE_MODE     = 0x0010,  // Primary domain is running in native mode
     DS_DOMAIN_DIRECT_INBOUND      = 0x0020   // Domain is directly trusting
}

[StructLayout(LayoutKind.Sequential)]
private struct DS_DOMAIN_TRUSTS
{
     [MarshalAs(UnmanagedType.LPTStr)]
     public string NetbiosDomainName;
     [MarshalAs(UnmanagedType.LPTStr)]
     public string DnsDomainName;
     public uint Flags;
     public uint ParentIndex;
     public uint TrustType;
     public uint TrustAttributes;
     public IntPtr DomainSid;
     public Guid DomainGuid;
}

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

Domains is an out parameter that receives a pointer to an array of DS_DOMAIN_TRUSTS structures. Each structure in this array contains trust data about a domain. The caller must free this memory when it is no longer required by calling NetApiBufferFree.

Tips & Tricks:

Please add some!

Sample Code:

public class DomainLister
{
     public static DS_DOMAIN_TRUSTS[] GetTrustedDomains()
     {
     // What trust types are we interested in ?
     uint trustTypes = (uint)(DS_DOMAIN_TRUST_TYPE.DS_DOMAIN_PRIMARY | DS_DOMAIN_TRUST_TYPE.DS_DOMAIN_DIRECT_OUTBOUND);

     IntPtr buf = new IntPtr();
     uint numDomains = 0;
     DS_DOMAIN_TRUSTS[] trusts = new DS_DOMAIN_TRUSTS[0];

     // Make the call - not doing anything special with the result value here
     uint result = DsEnumerateDomainTrusts(null,
                           trustTypes,
                           out buf,
                           out numDomains);

     try
     {
         if((numDomains > 0) && (result == 0))
         {
         // Marshal the received buffer to managed structs

         trusts = new DS_DOMAIN_TRUSTS[numDomains];

         IntPtr iter = buf;

         for(int i=0; i < numDomains; i++)
         {
             trusts[i] = (DS_DOMAIN_TRUSTS)Marshal.PtrToStructure(iter, typeof(DS_DOMAIN_TRUSTS));
             iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(DS_DOMAIN_TRUSTS)));
         }
         }
     }
     finally
     {
         // Make sure we free the buffer whatever happens
         NetApiBufferFree(buf);
     }

     return trusts;
     }

     [StructLayout(LayoutKind.Sequential)]
     public struct DS_DOMAIN_TRUSTS
     {
     [MarshalAs(UnmanagedType.LPTStr)]
     public string NetbiosDomainName;
     [MarshalAs(UnmanagedType.LPTStr)]
     public string DnsDomainName;
     public uint Flags;
     public uint ParentIndex;
     public uint TrustType;
     public uint TrustAttributes;
     public IntPtr DomainSid;
     public Guid DomainGuid;
     }

     [Flags]
     private enum DS_DOMAIN_TRUST_TYPE : uint
     {
     DS_DOMAIN_IN_FOREST       = 0x0001,  // Domain is a member of the forest
     DS_DOMAIN_DIRECT_OUTBOUND     = 0x0002,  // Domain is directly trusted
     DS_DOMAIN_TREE_ROOT       = 0x0004,  // Domain is root of a tree in the forest
     DS_DOMAIN_PRIMARY         = 0x0008,  // Domain is the primary domain of queried server
     DS_DOMAIN_NATIVE_MODE     = 0x0010,  // Primary domain is running in native mode
     DS_DOMAIN_DIRECT_INBOUND      = 0x0020   // Domain is directly trusting
     }

     [DllImport("Netapi32.dll", CallingConvention=CallingConvention.Winapi, SetLastError=true, CharSet=CharSet.Auto)]
     private static extern uint DsEnumerateDomainTrusts(string ServerName,
     uint Flags,
     out IntPtr Domains,
     out uint DomainCount);

     [DllImport("Netapi32.dll",EntryPoint="NetApiBufferFree")]
     private static extern uint NetApiBufferFree(IntPtr buffer);
}

Documentation